********************************************************************************
** 	TITLE:		th2014_gles_contact_networks                                  ** 	
**  AUTHOR:	    Philippe Mongrain                                             **
**	DATA:       ZA5740_v2-0-0                                                 **
**  DATE:	    October 2022 						                          **	
**	VERSION:	Stata 16					                                  **	
********************************************************************************

* Version control

version 16.0

* Open log file

capture log close       			  			              
log using "th2014_gles_contact_networks", replace

* Open the dataset

use "ZA5740_v2-0-0.dta", clear


***************************
** INDEPENDENT VARIABLES **
***************************

* Time

foreach var of varlist endtime {
g `var'_date = date(`var', "DMYhms")
replace `var'_date = date(`var', "DMY") if `var'_date==. & `var'!=""
format `var'_date %td
g `var'_time = clock(`var', "DMYhms")
format `var'_time %tc_HH:MM:SS
}

generate surveydate = endtime_date

format %tdMon_DD,_CCYY surveydate

gen edate = 20140914

gen electiondate=date(string(edate,"%8.0f"),"YMD")

format %tdMon_DD,_CCYY electiondate

gen time = electiondate - surveydate

* Gender

gen male = l1

recode male (2=0)

* Age

gen age = 2014 - l2

replace age = . if age < 17

* Education

gen education = l3

recode education (9=.)

* Interest for election

gen interest = l33

recode interest (-99=.)

replace interest = 6 - interest

* Coalition preference

gen coalprefA = l108a
gen coalprefB = l108b
gen coalprefC = l108c
gen coalprefD = l108d
gen coalprefE = l108e
gen coalprefF = l108f
gen coalprefG = l108g
gen coalprefH = l108h

recode coalprefA coalprefB coalprefC coalprefD coalprefE coalprefF coalprefG coalprefH (-99=.)

gen coalpref_complete = 1 if coalprefA!=. & coalprefB!=. & coalprefC!=. & coalprefD!=. & coalprefE!=. & coalprefF!=. & coalprefG!=. & coalprefH!=.

gen coalpref = 3 if coalprefE >= coalprefA & coalprefE >= coalprefB & coalprefE >= coalprefC & coalprefE >= coalprefD & coalprefE >= coalprefF & coalprefE >= coalprefG & coalprefE >= coalprefH & coalpref_complete == 1
replace coalpref = 2 if coalprefA == coalprefB & coalprefA == coalprefC & coalprefA == coalprefD & coalprefA == coalprefE & coalprefA == coalprefF & coalprefA == coalprefG & coalprefA == coalprefH & coalpref_complete == 1
replace coalpref = 1 if coalpref!=2 & coalpref!=3 & coalpref_complete == 1

replace coalpref = . if coalpref == 3 & coalprefE == coalprefA & coalpref_complete == 1
replace coalpref = . if coalpref == 3 & coalprefE == coalprefB & coalpref_complete == 1
replace coalpref = . if coalpref == 3 & coalprefE == coalprefC & coalpref_complete == 1
replace coalpref = . if coalpref == 3 & coalprefE == coalprefD & coalpref_complete == 1
replace coalpref = . if coalpref == 3 & coalprefE == coalprefF & coalpref_complete == 1
replace coalpref = . if coalpref == 3 & coalprefE == coalprefG & coalpref_complete == 1
replace coalpref = . if coalpref == 3 & coalprefE == coalprefH & coalpref_complete == 1

label define coalpref 1 "Loser coalition" 2 "No preference" 3 "Winner coalition"
label values coalpref coalpref

* Coalition preference strength

gen coalstrengthA = coalprefA if coalprefA >= coalprefB & coalprefA >= coalprefC & coalprefA >= coalprefD & coalprefA >= coalprefE & coalprefA >= coalprefF & coalprefA >= coalprefG & coalprefA >= coalprefH & coalpref_complete == 1
gen coalstrengthB = coalprefB if coalprefB >= coalprefA & coalprefB >= coalprefC & coalprefB >= coalprefD & coalprefB >= coalprefE & coalprefB >= coalprefF & coalprefB >= coalprefG & coalprefB >= coalprefH & coalpref_complete == 1
gen coalstrengthC = coalprefC if coalprefC >= coalprefA & coalprefC >= coalprefB & coalprefC >= coalprefD & coalprefC >= coalprefE & coalprefC >= coalprefF & coalprefC >= coalprefG & coalprefC >= coalprefH & coalpref_complete == 1
gen coalstrengthD = coalprefD if coalprefD >= coalprefA & coalprefD >= coalprefB & coalprefD >= coalprefC & coalprefD >= coalprefE & coalprefD >= coalprefF & coalprefD >= coalprefG & coalprefD >= coalprefH & coalpref_complete == 1
gen coalstrengthE = coalprefE if coalprefE >= coalprefA & coalprefE >= coalprefB & coalprefE >= coalprefC & coalprefE >= coalprefD & coalprefE >= coalprefF & coalprefE >= coalprefG & coalprefE >= coalprefH & coalpref_complete == 1
gen coalstrengthF = coalprefF if coalprefF >= coalprefA & coalprefF >= coalprefB & coalprefF >= coalprefC & coalprefF >= coalprefD & coalprefF >= coalprefE & coalprefF >= coalprefG & coalprefF >= coalprefH & coalpref_complete == 1
gen coalstrengthG = coalprefG if coalprefG >= coalprefA & coalprefG >= coalprefB & coalprefG >= coalprefC & coalprefG >= coalprefD & coalprefG >= coalprefE & coalprefG >= coalprefF & coalprefG >= coalprefH & coalpref_complete == 1
gen coalstrengthH = coalprefH if coalprefH >= coalprefA & coalprefH >= coalprefB & coalprefH >= coalprefC & coalprefH >= coalprefD & coalprefH >= coalprefE & coalprefH >= coalprefF & coalprefH >= coalprefG & coalpref_complete == 1

gen coalstrength = max(coalstrengthA,coalstrengthB,coalstrengthC,coalstrengthD,coalstrengthE,coalstrengthF,coalstrengthG,coalstrengthH)

replace coalstrength = 300 if coalstrength == 8 | coalstrength == 9 | coalstrength == 10 | coalstrength == 11
replace coalstrength = 200 if coalstrength == 5 | coalstrength == 6 | coalstrength == 7
replace coalstrength = 100 if coalstrength == 1 | coalstrength == 2 | coalstrength == 3 | coalstrength == 4

replace coalstrength = 3 if coalstrength == 300
replace coalstrength = 2 if coalstrength == 200
replace coalstrength = 1 if coalstrength == 100

label define coalstrength 1 "Weak preference" 2 "Moderate preference" 3 "Strong preference"
label values coalstrength coalstrength

* Coalition preference scale

gen coalscale = .

replace coalscale = 1 if coalpref == 1 & coalstrength == 3
replace coalscale = 2 if coalpref == 1 & coalstrength == 2
replace coalscale = 3 if coalpref == 1 & coalstrength == 1
replace coalscale = 4 if coalpref == 2
replace coalscale = 5 if coalpref == 3 & coalstrength == 1
replace coalscale = 6 if coalpref == 3 & coalstrength == 2
replace coalscale = 7 if coalpref == 3 & coalstrength == 3

label define coalscale 1 "Strong loser preference" 2 "Moderate loser preference" 3 "Weak loser preference" 4 "No preference" 5 "Weak winner preference" 6 "Moderate winner preference" 7 "Strong winner preference" 
label values coalscale coalscale

* Political discussion

gen discussion = l148

recode discussion (-97=.)
recode discussion (-99=.)

replace discussion = discussion - 1

gen discussion_3pts = 0 if discussion < 2 & discussion!=.
replace discussion_3pts = 1 if discussion >= 2 & discussion < 4 & discussion!=.
replace discussion_3pts = 2 if discussion >= 4 & discussion!=.

* Network size

gen size = l318

recode size (-97=.)
recode size (-99=.)

replace size = 0 if discussion == 0

gen size_3pts = size
replace size_3pts = 0 if size == 1
replace size_3pts = 1 if size == 2
replace size_3pts = 1 if size == 3
replace size_3pts = 1 if size == 4
replace size_3pts = 2 if size > 4 & size!=.

* Network expertise

gen expertise = l151

recode expertise (-97=.)
recode expertise (-99=.)

replace expertise = 4 - expertise

replace expertise = 0 if size == 0

* Political disagreement

gen disagreement = l152

recode disagreement (-97=.)
recode disagreement (-99=.)

replace disagreement = 4 - disagreement

replace disagreement = 0 if size == 0

gen disagreement_3pts = disagreement
replace disagreement_3pts = 0 if disagreement == 1
replace disagreement_3pts = 1 if disagreement == 2
replace disagreement_3pts = 2 if disagreement == 3


*********************************
** NATIONAL-LEVEL EXPECTATIONS ** 
*********************************

* Generate variable identifying incomplete answers

gen winCoalitionA = l107a
gen winCoalitionB = l107b
gen winCoalitionC = l107c
gen winCoalitionD = l107d
gen winCoalitionE = l107e
gen winCoalitionF = l107f
gen winCoalitionG = l107g
gen winCoalitionH = l107h

recode winCoalitionA winCoalitionB winCoalitionC winCoalitionD winCoalitionE winCoalitionF winCoalitionG winCoalitionH (-97=.)
recode winCoalitionA winCoalitionB winCoalitionC winCoalitionD winCoalitionE winCoalitionF winCoalitionG winCoalitionH (-99=.)

gen complete_whole = 1 if winCoalitionA!=. & winCoalitionB!=. & winCoalitionC!=. & winCoalitionD!=. & winCoalitionE!=. & winCoalitionF!=. & winCoalitionG!=. & winCoalitionH!=.

* Generate value of 1 if the expectation for one coalition is higher or equal to the expectations for at least one other coalition

gen coalitionA = 1 if winCoalitionA >= winCoalitionB & winCoalitionA >= winCoalitionC & winCoalitionA >= winCoalitionD & winCoalitionA >= winCoalitionE & winCoalitionA >= winCoalitionF & winCoalitionA >= winCoalitionG & winCoalitionA >= winCoalitionH & complete_whole == 1

gen coalitionB = 1 if winCoalitionB >= winCoalitionA & winCoalitionB >= winCoalitionC & winCoalitionB >= winCoalitionD & winCoalitionB >= winCoalitionE & winCoalitionB >= winCoalitionF & winCoalitionB >= winCoalitionG & winCoalitionB >= winCoalitionH & complete_whole == 1

gen coalitionC = 1 if winCoalitionC >= winCoalitionA & winCoalitionC >= winCoalitionB & winCoalitionC >= winCoalitionD & winCoalitionC >= winCoalitionE & winCoalitionC >= winCoalitionF & winCoalitionC >= winCoalitionG & winCoalitionC >= winCoalitionH & complete_whole == 1

gen coalitionD = 1 if winCoalitionD >= winCoalitionA & winCoalitionD >= winCoalitionB & winCoalitionD >= winCoalitionC & winCoalitionD >= winCoalitionE & winCoalitionD >= winCoalitionF & winCoalitionD >= winCoalitionG & winCoalitionD >= winCoalitionH & complete_whole == 1

gen coalitionE = 1 if winCoalitionE >= winCoalitionA & winCoalitionE >= winCoalitionB & winCoalitionE >= winCoalitionC & winCoalitionE >= winCoalitionD & winCoalitionE >= winCoalitionF & winCoalitionE >= winCoalitionG & winCoalitionE >= winCoalitionH & complete_whole == 1

gen coalitionF = 1 if winCoalitionF >= winCoalitionA & winCoalitionF >= winCoalitionB & winCoalitionF >= winCoalitionC & winCoalitionF >= winCoalitionD & winCoalitionF >= winCoalitionE & winCoalitionF >= winCoalitionG & winCoalitionF >= winCoalitionH & complete_whole == 1

gen coalitionG = 1 if winCoalitionG >= winCoalitionA & winCoalitionG >= winCoalitionB & winCoalitionG >= winCoalitionC & winCoalitionG >= winCoalitionD & winCoalitionG >= winCoalitionE & winCoalitionG >= winCoalitionF & winCoalitionG >= winCoalitionH & complete_whole == 1

gen coalitionH = 1 if winCoalitionH >= winCoalitionA & winCoalitionH >= winCoalitionB & winCoalitionH >= winCoalitionC & winCoalitionH >= winCoalitionD & winCoalitionH >= winCoalitionE & winCoalitionH >= winCoalitionF & winCoalitionH >= winCoalitionG & complete_whole == 1

gen coalitionOTH = 1 if winCoalitionA == 1 & winCoalitionB == 1 & winCoalitionC == 1 & winCoalitionD == 1 & winCoalitionE == 1 & winCoalitionF == 1 & winCoalitionG == 1 & winCoalitionH == 1 & complete_whole == 1

replace coalitionA = . if coalitionOTH == 1 & complete_whole == 1
replace coalitionB = . if coalitionOTH == 1 & complete_whole == 1
replace coalitionC = . if coalitionOTH == 1 & complete_whole == 1
replace coalitionD = . if coalitionOTH == 1 & complete_whole == 1
replace coalitionE = . if coalitionOTH == 1 & complete_whole == 1
replace coalitionF = . if coalitionOTH == 1 & complete_whole == 1
replace coalitionG = . if coalitionOTH == 1 & complete_whole == 1
replace coalitionH = . if coalitionOTH == 1 & complete_whole == 1

* Generate variable for ambiguous forecasts

gen ambiguous_whole = .

replace ambiguous_whole = 99 if coalitionA == coalitionB & coalitionA!=. & complete_whole == 1 | coalitionA == coalitionC & coalitionA!=. & complete_whole == 1 | coalitionA == coalitionD & coalitionA!=. & complete_whole == 1 | coalitionA == coalitionE & coalitionA!=. & complete_whole == 1 | coalitionA == coalitionF & coalitionA!=. & complete_whole == 1 | coalitionA == coalitionG & coalitionA!=. & complete_whole == 1 | coalitionA == coalitionH & coalitionA!=. & complete_whole == 1

replace ambiguous_whole = 99 if coalitionB == coalitionA & coalitionB!=. & complete_whole == 1 | coalitionB == coalitionC & coalitionB!=. & complete_whole == 1 | coalitionB == coalitionD & coalitionB!=. & complete_whole == 1 | coalitionB == coalitionE & coalitionB!=. & complete_whole == 1 | coalitionB == coalitionF & coalitionB!=. & complete_whole == 1 | coalitionB == coalitionG & coalitionB!=. & complete_whole == 1 | coalitionB == coalitionH & coalitionB!=. & complete_whole == 1

replace ambiguous_whole = 99 if coalitionC == coalitionA & coalitionC!=. & complete_whole == 1 | coalitionC == coalitionB & coalitionC!=. & complete_whole == 1 | coalitionC == coalitionD & coalitionC!=. & complete_whole == 1 | coalitionC == coalitionE & coalitionC!=. & complete_whole == 1 | coalitionC == coalitionF & coalitionC!=. & complete_whole == 1 | coalitionC == coalitionG & coalitionC!=. & complete_whole == 1 | coalitionC == coalitionH & coalitionC!=. & complete_whole == 1

replace ambiguous_whole = 99 if coalitionD == coalitionA & coalitionD!=. & complete_whole == 1 | coalitionD == coalitionB & coalitionD!=. & complete_whole == 1 | coalitionD == coalitionC & coalitionD!=. & complete_whole == 1 | coalitionD == coalitionE & coalitionD!=. & complete_whole == 1 | coalitionD == coalitionF & coalitionD!=. & complete_whole == 1 | coalitionD == coalitionG & coalitionD!=. & complete_whole == 1 | coalitionD == coalitionH & coalitionD!=. & complete_whole == 1

replace ambiguous_whole = 99 if coalitionE == coalitionA & coalitionE!=. & complete_whole == 1 | coalitionE == coalitionB & coalitionE!=. & complete_whole == 1 | coalitionE == coalitionC & coalitionE!=. & complete_whole == 1 | coalitionE == coalitionD & coalitionE!=. & complete_whole == 1 | coalitionE == coalitionF & coalitionE!=. & complete_whole == 1 | coalitionE == coalitionG & coalitionE!=. & complete_whole == 1 | coalitionE == coalitionH & coalitionE!=. & complete_whole == 1

replace ambiguous_whole = 99 if coalitionF == coalitionA & coalitionF!=. & complete_whole == 1 | coalitionF == coalitionB & coalitionF!=. & complete_whole == 1 | coalitionF == coalitionC & coalitionF!=. & complete_whole == 1 | coalitionF == coalitionD & coalitionF!=. & complete_whole == 1 | coalitionF == coalitionE & coalitionF!=. & complete_whole == 1 | coalitionF == coalitionG & coalitionF!=. & complete_whole == 1 | coalitionF == coalitionH & coalitionF!=. & complete_whole == 1

replace ambiguous_whole = 99 if coalitionG == coalitionA & coalitionG!=. & complete_whole == 1 | coalitionG == coalitionB & coalitionG!=. & complete_whole == 1 | coalitionG == coalitionC & coalitionG!=. & complete_whole == 1 | coalitionG == coalitionD & coalitionG!=. & complete_whole == 1 | coalitionG == coalitionE & coalitionG!=. & complete_whole == 1 | coalitionG == coalitionF & coalitionG!=. & complete_whole == 1 | coalitionG == coalitionH & coalitionG!=. & complete_whole == 1

replace ambiguous_whole = 99 if coalitionH == coalitionA & coalitionH!=. & complete_whole == 1 | coalitionH == coalitionB & coalitionH!=. & complete_whole == 1 | coalitionH == coalitionC & coalitionH!=. & complete_whole == 1 | coalitionH == coalitionD & coalitionH!=. & complete_whole == 1 | coalitionH == coalitionE & coalitionH!=. & complete_whole == 1 | coalitionH == coalitionF & coalitionH!=. & complete_whole == 1 | coalitionH == coalitionG & coalitionH!=. & complete_whole == 1

* Generate value of 0 for lower expectations

replace coalitionA = 0 if coalitionA!=1 & coalitionA!=. & complete_whole == 1
replace coalitionB = 0 if coalitionB!=1 & coalitionB!=. & complete_whole == 1
replace coalitionC = 0 if coalitionC!=1 & coalitionC!=. & complete_whole == 1
replace coalitionD = 0 if coalitionD!=1 & coalitionD!=. & complete_whole == 1
replace coalitionE = 0 if coalitionE!=1 & coalitionE!=. & complete_whole == 1
replace coalitionF = 0 if coalitionF!=1 & coalitionF!=. & complete_whole == 1
replace coalitionG = 0 if coalitionG!=1 & coalitionG!=. & complete_whole == 1
replace coalitionH = 0 if coalitionH!=1 & coalitionH!=. & complete_whole == 1
replace coalitionOTH = 0 if coalitionOTH!=1 & coalitionOTH!=. & complete_whole == 1

* Forecasts (1 = SPD-CDU, 2 = SPD-FDP-Green, 3 = SPD-Green, 4 = SPD-The Left-Green, 5 = SPD-FDP, 6 = CDU-FDP-Green, 7 = CDU-Green, 8 = CDU-FDP, 88 = other, 99 = ambiguous)

gen forecast_whole = .

replace forecast_whole = 1 if coalitionA == 1 & complete_whole == 1
replace forecast_whole = 2 if coalitionB == 1 & complete_whole == 1
replace forecast_whole = 3 if coalitionC == 1 & complete_whole == 1
replace forecast_whole = 4 if coalitionD == 1 & complete_whole == 1
replace forecast_whole = 5 if coalitionE == 1 & complete_whole == 1
replace forecast_whole = 6 if coalitionF == 1 & complete_whole == 1
replace forecast_whole = 7 if coalitionG == 1 & complete_whole == 1
replace forecast_whole = 8 if coalitionH == 1 & complete_whole == 1
replace forecast_whole = 88 if coalitionOTH == 1 & complete_whole == 1
replace forecast_whole = 99 if ambiguous_whole == 1 & complete_whole == 1

* Identify correct and ambiguous forecasts

gen correct_whole = 0 if forecast_whole!=5 & forecast_whole!=. & complete_whole == 1
replace correct_whole = 1 if forecast_whole == 5 & complete_whole == 1

replace correct_whole = 99 if forecast_whole == 99 & coalitionE == 1 & complete_whole == 1

* Generate dichotomous variable (treat ambiguous forecasts as incorrect)

gen correct_whole_d = correct_whole

replace correct_whole_d = 0 if correct_whole_d == 99
replace correct_whole_d = 1 if correct_whole_d == 1


**********
** SAVE **
**********

save "th2014_gles.dta", replace


*******************
** MAIN ANALYSES **
*******************

* Regression analysis and percentage of correct and incorrect forecasts

logistic correct_whole_d discussion disagreement size expertise coalscale interest age i.male education time  
estimates table, star(.05 .01 .001)

tab correct_whole_d if e(sample) == 1


************************
** SUMMARY STATISTICS **
************************

label variable correct_whole_d "Coalition formed (DV)"
label variable discussion "Discussion"
label variable disagreement "Disagreement"
label variable size "Network size"
label variable expertise "Network expertise"
label variable coalscale "Coalition preference"
label variable interest "Interest"
label variable age "Age"
label variable male "Gender (male = 1)"
label variable education "Education"
label variable time "Time of interview"

estpost tabstat correct_whole_d discussion disagreement size expertise coalscale interest age male education time if e(sample) == 1, statistics(n mean p50 sd min max) columns(statistics)

esttab using "th2014_gles_summary_regional.tex", substitute("\begin{table}[htbp]" "\begin{table}[H]") cells("count(label(N) fmt(%9.0fc)) mean(fmt(%5.2f) label(Mean)) p50(fmt(%5.1f) label(Median)) sd(fmt(%5.1f) label(Std. dev.)) min(fmt(%5.1f) label(Min)) max(fmt(%5.1f) label(Max))") label width(\textwidth) nomtitle nonumber noobs booktabs title("Summary statistics -- 2014 Thuringian regional election (GLES), regional level") replace

eststo clear

log close